package com.cangxuan.app.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 基类 Activity
 */
public class BaseActivity extends AppCompatActivity {

    public static Context context;    //上下文对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置的状态栏字体为暗色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        //获取上下文对象
        context = getApplicationContext();
    }

    @Override
    public void setTitle(CharSequence title) {
        ActionBar actionBar = getSupportActionBar();
        //启用返回箭头
        actionBar.setDisplayHomeAsUpEnabled(true);
        //动态创建一个TextView
        TextView txtTitle = new TextView(context);
        txtTitle.setText(title);
        txtTitle.setTextSize(20);
        txtTitle.setTextColor(Color.BLACK);
        txtTitle.setGravity(Gravity.CENTER);

        //设置actionBar的自定义视图
        actionBar.setCustomView(txtTitle,
                new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
                        ActionBar.LayoutParams.MATCH_PARENT,
                        Gravity.CENTER));
        //设置actionBar的显示选项
        actionBar.setDisplayOptions(
                ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE,
                ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE);

        //隐藏自带Title
        actionBar.setDisplayShowTitleEnabled(false);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
